class GameLevel extends eui.Component implements eui.UIComponent {
	public constructor() {
		super();
	}
	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}
	protected childrenCreated(): void {
		super.childrenCreated();
		GameLevel._ins = this;

		this.init();
	}
	
	// 单例模式
	public static get ins(): GameLevel { return GameLevel._ins;	}

	// 棋盘
	public get board(): XXLBoard { return this._board; }
	
	// 获取节点通过坐标
	public getNodeByPos(xPos: number, yPos: number): XXLNode {
		if(xPos < 0 || yPos < 0) return null;
		return this.board.getNodeByPos(xPos, yPos);
	}

	public getItemByNode(node: XXLNode): Item {
		if(node == null) return null;
		return this.getItemByPos(node.x_pos, node.y_pos);
	}

	public getItemByPos(x: number, y: number): Item {
		if(x < 0 || y < 0) return null;
		return this._items[y][x];
	}

	// 消除事件
	private onXcItem(en : string, xp : number, yp : number){
		let self = this;
		let item = self._items[yp][xp];

		// 
		console.log("执行渐隐！");

		JcEvent.ins.NotifyEvent(XXLEventName.onXCItem);
		let tw = egret.Tween.get(item);
		tw.to({alpha : 0}, 300).call(function(){
			item.clear();
			JcEvent.ins.NotifyEvent(XXLEventName.xcItemEnd);
		}).to({alpha : 1}, 10);

		// item.clear();
	}

	// 交换位置
	private onSwapeItem(en : string, node1 : XXLNode, node2 : XXLNode){
		if(node1 == null || node2 == null) return;
		let self = this;		
		self.swape(node1, node2);
	}

	// 下落
	private onDropItem(en : string, node1 : XXLNode, node2 : XXLNode){
		if(node1 == null || node2 == null) return;
		let self = this;

		let upItem = self._items[node1.y_pos][node1.x_pos];
		let downItem = self._items[node2.y_pos][node2.x_pos];
		if(upItem == null || downItem == null) return;
		
		let downYPos = downItem.y;
		let upYPos = upItem.y;
		let tempHeight = downItem.height;
		
		downItem.y = upYPos;
		console.log("--------------" + downItem.y);

		JcEvent.ins.NotifyEvent(XXLEventName.onDropItem);
		let tw = egret.Tween.get(upItem);
		let durationTime = (Math.floor(downYPos / tempHeight)) * 80;
		tw.to({y: downYPos}, durationTime).call(function(){
			let upX = node1.x_pos;
			let upY = node1.y_pos;

			let downX = node2.x_pos;
			let downY = node2.y_pos;

			upItem.swapeItem(downItem);
			self._items[downY][downX] = upItem;
			self._items[upY][upX] = downItem;
			
			JcEvent.ins.NotifyEvent(XXLEventName.dropItemEnd);

			let str = "上面item 的 Y = ";
			str += upYPos;
			str += ", 下面item 的 Y = ";
			str += downYPos;
			str += ", 下落后上面item 的 Y = ";
			str += upItem.y;
			str += ", 下落后下面item 的 Y = ";
			str += downItem.y;

			console.log(str);
		});
	}

	// 创建节点
	private onCreateItem(en : string, node : XXLNode){
		let self = this;
		let x = node.x_pos;
		let y = node.y_pos;
		let val = node.val;
		let item = self._items[y][x];
		item.val = val;
		
		let str = "创建消除块！node x = ";
		str += x;
		str += ", y =";
		str += y;
		str += ", item x = ";
		str += item._posX;
		str += ", item y = ";
		str += item._posY;
		str += ", item pos x = ";
		str += item.x;
		str += ", item pos y = ";
		str += item.y;
		str += ", color = ";
		str += item._color;
		str += ", png name = ";
		str += item.img_state1.source;

		console.log(str);
	}

	// 无法交换,播放一个声音,或者做其他表现
	private onDontSwape(){

	}

	// 交换两个item
	private swape(node1: XXLNode, node2: XXLNode){
		if(node1 == null || node2 == null) return;

		let self = this;
		let item1 : Item = self._items[node1.y_pos][node1.x_pos];
		let item2 : Item = self._items[node2.y_pos][node2.x_pos];		
		if(item1 == null || item2 == null) return;
		
		let v1 = new Vector2(item1.x, item1.y);
		let v2 = new Vector2(item2.x, item2.y);

		JcEvent.ins.NotifyEvent(XXLEventName.onSwapeItem);
		let tw1 = egret.Tween.get(item1);
		tw1.to({x : v2.x }, 300);
		tw1.to({y : v2.y }, 300);
		
		let tw2 = egret.Tween.get(item2);
		tw2.to({x : v1.x }, 300);
		tw2.to({y : v1.y }, 300).call(function(){
			item2.swapeItem(item1);
			self._items[node1.y_pos][node1.x_pos] = item2;
			self._items[node2.y_pos][node2.x_pos] = item1;
			JcEvent.ins.NotifyEvent(XXLEventName.swapeItemEnd);
			console.log("执行完交换！");
		});
	}	

	// private checkAll(){
	// 	JcEvent.ins.NotifyEvent(XXLEventName.checkAll);
	// }

	private onItemClick() {

	}

	// 初始化消除块
	private initItems() {
		let xCnt = 4;
		let yCnt = 4;
		let types = [1, 2, 3];

		let self = this;

		self._board = new XXLBoard(yCnt, xCnt, types);
		self.board.createNodes();

		self._items = new Array<Array<Item>>();
		for (let i = 0; i < yCnt; i++) {
			self._items[i] = [];
		}
		self._items.push([]);

		for (var i = 0; i < self.board.y_Cnt; i++) {
			for (var j = 0; j < self.board.x_Cnt; j++) {
				var node = self.board.nodes[i][j];
				var item = new Item(node.val, self.onItemClick, j, i);
				self._items[i][j] = item;
				self.addChild(item);
				item.x = j * 80;
				item.y = i * 80;
			}
		}

		// 延迟1.5S后开始检查能否消除
		// egret.setTimeout(this.checkAll, self, 1500);
	}

	private initEvent(){
		let jcEvent = JcEvent.ins;
		let self = this;
		jcEvent.RegistEvent(XXLEventName.xcItem, self.onXcItem, self);
		jcEvent.RegistEvent(XXLEventName.swapeItem, self.onSwapeItem, self);
		jcEvent.RegistEvent(XXLEventName.dropItem, self.onDropItem, self);
		jcEvent.RegistEvent(XXLEventName.createItem, self.onCreateItem, self);
		jcEvent.RegistEvent(XXLEventName.dontSwape, self.onDontSwape, self);
	}

	private removeEvent(){
		let jcEvent = JcEvent.ins;
		let self = this;

		jcEvent.RemoveEvent(XXLEventName.xcItem, self.onXcItem, self);
		jcEvent.RemoveEvent(XXLEventName.swapeItem, self.onSwapeItem, self);
		jcEvent.RemoveEvent(XXLEventName.dropItem, self.onDropItem, self);
		jcEvent.RemoveEvent(XXLEventName.createItem, self.onCreateItem, self);
		jcEvent.RemoveEvent(XXLEventName.dontSwape, self.onDontSwape, self);
	}

	private init() {
		let self = this;

		self.initEvent();
		self.initItems();

		self._itemGroup = new ItemGroup(this);
		self.addChild(this._itemGroup);
		self._itemGroup.x = 0;
		self._itemGroup.y = 0;
	}

	private _board: XXLBoard;
	private _itemGroup: ItemGroup;
	private _items: Array<Array<Item>>;

	private static _ins: GameLevel;
}